﻿package Modules.PlayerModule
{
	import Modules.CommunicationsModule.*;
	import Modules.MainTimeLine;
	
	public class PlayerUtil
	{	
		
		public static var loginsigninlapse:int;
		public static var levelUpRewards:Array;
		
		/*public static var expArray:Array = [0, 0,
			30,	90,	170,	270,	420,	637,	942,
			1326,	1800,	2369,	3041,	3822,	4719,
			5737,	6881,	8155,	9564,	11111,	12800,	14632,
			16610,	18737,	21012,	23437,	26012,	28737,	31610,	34632,
			37800,	41111,	44564,	48155,	51881,	55737,	59719,	63822,
			68041,	72369,	76800,	81326,	85942,	90637,	95406,	100237,
			105122,	110052,	115015,	120001,	125000, 131324,	137795,	144410,
			151165,	158056,	165079,	172229,	179503,	186894,	194400,	202013,
			209728,	217540,	225443,	233431,	241496,	249633,	257834,	267406,
			276458,	286328,	296358,	305767,	316074,	326531,	336255,	346965,
			357812,	367807,	378880,	390077,	400293,	411686,	423190,	433572,
			445239,	457001,	467489,	479378,	491346,	501878,	513934,	526049,
			536557,	548720,	560922,	571333,	583539,	591882,	600000];*/

		
		/*public static var expArray:Array = [0,			//0
			600,		//1
			1000,		//2
			1600,		//3
			2400,		//4
			3400,		//5
			4500,		//6
			5700,		//7
			7000,		//8
			8500,		//9
			10100,		//10
			11800,		//11
			13600,		//12
			15500,		//13
			17500,		//14
			19600,		//15
			21800,		//16
			24100,		//17
			26500,		//18
			29000,		//19
			31600,		//20
			34300,		//21
			37100,		//22
			40000,		//23
			50000];	//24*/
		
		//creates a new player
		public static function initNewPlayer():void{
			
			var player:Player = MainTimeLine.session.player;
			
			player.diamond = 0;
			player.exp = 0;
			player.gender = 0;
			player.gold = 500;
			player.level = 1;
			player.stamina = getStaminaLimit(player.level);
			//player.partyName = "";
			//player.volunteerID = "";
			
			player.logInTime = MainTimeLine.serverTime;
			MainTimeLine.loadedPlayer = true;

			/*if(player.getPlayerID() && player.getPlayerID() != ""){
				FacebookCallBacks.loadProfile(player.getPlayerID(), true);
			}
			else{
				player.playerName = "Dev Admin";
				player.picture = null;
			}
			
			PartyCommunicationModule.loadPartyTable();*/
		}
		
		//modifies the player's statistics to that of the loaded player
		public static function loadPlayer(loadedData:Object):void{
			
			var player:Player = MainTimeLine.session.player;
			
			player.diamond = loadedData.diamond;
			player.exp = loadedData.exp; 
			player.gender = loadedData.gender;
			
			player.gold = loadedData.gold;
			
			player.level = loadedData.level;
			player.stamina = loadedData.stamina;
			
			player.logInTime = MainTimeLine.serverTime;
			
			player.farmName = loadedData.farmName;
			
			/*
			if(player.getPlayerID() && player.getPlayerID() != "")
				FacebookCallBacks.loadProfile(player.getPlayerID(), true);
			else{
				player.playerName = "Dev Admin";
				player.picture = null;
			}
			
			loginsigninlapse = player.logInTime - loadedData.lastLogin;	
			
			//Daily reward
			player.lastCount = loadedData.lastCount;
			var elapsed:int = player.logInTime - player.lastCount;
			player.count = loadedData.count;
			if (elapsed>12*60*60*1000&&elapsed<24*60*60*1000) {
				player.count++;
				player.lastCount = player.logInTime;
				player.dailyReward = true;
			} else if (player.lastCount == 0) {
				player.count = 1;
				player.lastCount = player.logInTime;
				player.dailyReward = true;
			} else if (elapsed >= 24*60*60*1000) {
				player.count = 1;
				player.lastCount = player.logInTime;
				player.dailyReward = false;
			} else if (elapsed<=12*60*60*1000) {
				player.dailyReward = false;
				trace("login time< half day");
			} else {
				player.dailyReward = false;
				trace("unknown case");
			}
			
			if (player.count>5)
				player.count = 5;
			
			// volunteer
			player.volunteerID = loadedData.volunteerID;
			
			if(player.volunteerID && player.volunteerID != ""){
				MainTimeLine.root.rootMC.townHallPanel.volunteerPanel.hideLoginButton();
				
				//if(MainTimeLine.loadedVolunteerEvents && MainTimeLine.loadedOrientationEvents){
				//	MainTimeLine.session.player.volunteer.checkAllUserActivityStatus();
				//	MainTimeLine.session.player.volunteer.checkAllUserOrientationStatus();	
				//}
			}
			
			// party
			player.partyName = loadedData.partyName;
			
			PartyCommunicationModule.loadPartyTable();
			
			MainTimeLine.root.rootMC.refreshStatus();
			
			ClientServerCommunications.loadPlayerVolunteerReward();*/
		}
		
		public static function login():void{
			
			var staminaToAdd:int = 0;
			var fiveMinutes:int = 5*60;
			var eightHours:int = 8*60*60;
				
			/*while(loginsigninlapse > fiveMinutes){
			
				staminaToAdd += 1* MainTimeLine.session.player.farm.staminaRecoveryModifer;
				loginsigninlapse -= fiveMinutes;
			}
			
			addStamina(staminaToAdd);*/
		}
		
		// calculate the full stamina at given level
		public static function getStaminaLimit(level:int):int{
			return 50 + (level - 1) * 5;
		}
		
		/*public static function addExp(amount:int):void{
			
			var player:Player = MainTimeLine.session.player;
			//trace(player.level + " " + levelUpRewards[player.level-1].experience);
			player.exp += amount;
			
			if (player.level<=levelUpRewards.length) {
				if (player.exp>=(levelUpRewards[player.level-1] as Object).experience)
					levelUp();
			}
			else {
				if (player.exp>=(levelUpRewards[levelUpRewards.length-1] as Object).experience)
					levelUp();
			}
		}
		
		public static function addGold(amount:int):Boolean{
			//trace(amount);
			var player:Player = MainTimeLine.session.player;
			
			if((player.gold + amount)<0){
				//trace ("Failed !! Gold = "+player.gold + " , Amount = "+amount);
				if (MainTimeLine.session.player.tut!=3)
					MainTimeLine.root.rootMC.insufficientPanel.showMessageBox("Uh oh... You do not have enough gold");
				return false;
			}else{
				//trace("Util gold + " +amount);
				player.gold += amount;
				//MainTimeLine.root.rootMC.goldAmountToAdd = amount;
				//MainTimeLine.root.rootMC.addGold(amount);
				//trace("Gold = "+player.gold);
				return true;
			}	
		}
		
		public static function addDiamond(amount:int):Boolean{
			
			var player:Player = MainTimeLine.session.player;
		
			if((player.diamond + amount)<0){
				
				return false;	
			}else{
				
				player.diamond += amount;
				//MainTimeLine.root.rootMC.diamondAmountToAdd = amount;
				//MainTimeLine.root.rootMC.addDiamond(amount);
				return true;
			}
		}
		
		public static function addStamina(amount:int):Boolean{
			
			var player:Player = MainTimeLine.session.player;
			
			if((player.stamina + amount)<0){
				MainTimeLine.root.rootMC.insufficientPanel.showMessageBox("Uh oh... You do not have enough stamina.");
				return false;	
			}else{
				
				player.stamina += amount;
				
				if(player.stamina > getStaminaLimit(player.level)){
					player.stamina = getStaminaLimit(player.level);
				}

//				trace("player stamina: " + player.stamina);
				//MainTimeLine.root.rootMC.staminaAmountToAdd = amount;
//				MainTimeLine.root.rootMC.addStamina(amount);
				return true;
			}
		}
		
		// level up effects done here
		public static function levelUp(){
			
			var player:Player = MainTimeLine.session.player;
			
			// increase level
			player.level +=1;
			
			// replenish stamina
			player.stamina = getStaminaLimit(player.level);
			
			// add gold
			if (player.level<=levelUpRewards.length)
				addGold((levelUpRewards[player.level-1] as Object).gold);
			else
				addGold((levelUpRewards[levelUpRewards.length-1] as Object).gold);
				
			// show level up visual
			//MainTimeLine.root.rootMC.isLevelUp = true; // if true, will not play updateStatus animation after action complete
		}*/
	}
}